<template>
  <div>
    <!--搜索表单-->
    <div style="margin-bottom: 10px"></div>
    <div>
      <el-input style="width: 150px;" placeholder="请输入名称" v-model="params.name" clearable></el-input>
      <el-input style="width: 200px;margin-left: 5px" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search">搜索</i></el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh-right">重置</i></el-button>
    </div>
    <div style="margin-bottom: 10px"></div>
    <el-table :data="tableData" stripe border>
      <el-table-column prop="id" label="编号" width="50%"></el-table-column>
      <el-table-column prop="username" label="会员卡号" width="150%"></el-table-column>
      <el-table-column prop="name" label="名称" width="80%"></el-table-column>
      <el-table-column prop="age" label="年龄" width="50%"></el-table-column>
      <el-table-column prop="sex" label="性别" width="50%"></el-table-column>
      <el-table-column prop="phone" label="电话" width="100%"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="account" label="账户积分"></el-table-column>
      <el-table-column label="状态" width="230">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              @change="changeStatus(scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间" width="100%"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间" width="100%"></el-table-column>

      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="primary" @click="$router.push('/editUser?id='+scope.row.id)">编辑</el-button>
          <el-button type="success" @click="RechargeHandel(scope.row)">充值</el-button>
          &nbsp;&nbsp;
          <el-popconfirm
              title="确认删除这行数据吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <div style="margin-top: 20px"></div>
    <!--  分页  -->
    <div>
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next,jumper,->,total"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

    <div id="充值对话框">
      <el-dialog title="会员积分充值" :visible.sync="dialogFormVisible" style="text-align: center;width: 500px;margin: 30px auto;">
        <el-form :model="form" :rules="rules" ref="reChangeRef">
          <el-form-item prop="score" label="当前积分">
            <el-input v-model="form.score"disabled></el-input>
          </el-form-item>
          <el-form-item prop="account">
            <el-input v-model="form.account" placeholder="请输入充值金额"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelRecharge">取 消</el-button>
          <el-button type="primary" @click="Recharge">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import request from "@/utils/request";

export default {
  name: 'User',
  data(){
    const checkRechange = (rules,value,callback) =>{
      value = parseInt(value)
      if (!value){
        callback(new Error("请输充值积分"))
      }
      if(value<=0 || !Number.isInteger(value)){
        callback(new Error("请输入合法积分"))
      }
      callback()
    }
    return{
      tableData: [],
      total:0,
      dialogFormVisible:false,
      params:{
        pageNum:1,
        pageSize:10,
        name:'',
        phone:''
      },
      form:{},
      rules:{
        account:[{validator:checkRechange,trigger:blur()}]
      }
    }
  },
  created() {
    this.load()
  },
  methods:{
    changeStatus(row){
      request.put("user/update",row).then(res =>{
        if (res.code === '200'){
          this.$notify.success("操作成功！")
          this.load()
        }else{
          this.$notify.error(res.msg)
        }
      })
    },
    load(){
      /*fetch("http://localhost:9090/user/list").then(res => res.json()).then(res =>{
      console.log(res);
      this.tableData = res;
      })*/
      request.get('/user/page',{params:this.params}).then(res =>{
        if (res.code == '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    cancelRecharge(){
      this.$refs['reChangeRef'].resetFields()
      this.dialogFormVisible = false
    },
    RechargeHandel(row){
      this.dialogFormVisible = true
      this.form.score = row.account
      this.form["id"] = row.id
    },
    Recharge(){
      this.$refs["reChangeRef"].validate(valid => {
        if(valid){
          request.post('user/recharge',this.form).then(result => {
            if (result.code === '200'){
              this.$message.success("恭喜您充值完成！")
              this.$refs['reChangeRef'].resetFields()
              this.dialogFormVisible = false;
              this.load()
            }else {
              this.$message.error("充值失败！")
              this.$refs['reChangeRef'].resetFields()
              this.dialogFormVisible = false;
            }
          })
        }
      })

    },
    reset(){
      this.params={
        pageNum:1,
        pageSize:10,
        name:'',
        phone:''
      }
      this.load()
    },
    handleCurrentChange(pageNum){
      //点击分页按钮出发分页
      this.params.pageNum = pageNum
      this.load()
    },
    del(id){
      request.delete("/user/delete/" + id).then(res =>{
        if (res.code === '200'){
          this.$notify.success("删除OK!")
          this.load()
        }else {
          this.$notify.error(res.code)
        }
      })
    }
  }

}
</script>

<style scoped>

</style>
